<!--客户信息-->
<template>
  <div class="app-container erha-content">
    <div class="erha-header">
      <el-form :inline="true" :model="queryForm" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="queryForm.userName" clearable placeholder="客户名称" />
        </el-form-item>
        <el-form-item>
          <el-select v-model="queryForm.userType" clearable placeholder="客户类型">
            <el-option label="企业" value="企业" />
            <el-option label="个人" value="个人" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryTable">查询</el-button>
          <!--          <el-button type="primary" @click="openAddDialog()">新增</el-button>-->
        </el-form-item>
      </el-form>
    </div>

    <div class="erha-body">
      <el-table
        :data="table.data"
        height="600"
        style="width: 100%;"
      >
        <el-table-column prop="userName" label="客户名称" min-width="180" />
        <el-table-column prop="userType" label="客户类型" min-width="180" />
        <el-table-column prop="idNumber" label="客户身份证" min-width="180" />
        <el-table-column prop="phoneCode" label="联系电话" min-width="180" />
        <el-table-column prop="userAddress" label="联系地址" min-width="180" />
        <el-table-column prop="userScore" label="评级" min-width="180">
          <template v-slot="{row}">
            <el-tag v-if="row.userScore > 90" effect="dark" type="success">一级({{ row.userScore }})分</el-tag>
            <el-tag v-else-if="row.userScore >= 80 && row.userScore <= 90" effect="dark">二级({{ row.userScore }})分</el-tag>
            <el-tag v-else-if="row.userScore && row.userScore < 80" effect="dark" type="warning">三级({{ row.userScore }})分</el-tag>
            <el-tag v-else type="info" effect="dark">未评分</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" min-width="130">
          <template v-slot="{row}">
            <el-button type="text" @click="grade(row)">评级</el-button>
          </template>
        </el-table-column>

      </el-table>
    </div>

  </div>
</template>

<script>
import {
  queryUserInfo,
  queryUserInfoList,
  saveContractInfo,
  saveUserInfo,
  selectContractInfo,
  updateStatus,
  userInsuranceInfo
} from '@/api/customerInfo'

export default {
  name: 'CustomerInfo',
  data() {
    return {
      queryForm: {},

      table: {
        data: [],
        loading: false
      },

      dialog: {
        add: {
          title: '添加客户信息',
          loading: false,
          show: false,
          editSwitch: true,
          form: {
            userInfoProperty: {}
          }
        },
        contract: {
          title: '合同信息',
          loading: false,
          show: false,
          previewSwitch: false,
          form: {
            contractCode: ''
          },
          row: {}
        },
        insurancePolicy: {
          title: '保单信息',
          loading: false,
          show: false,
          form: {}
        }
      },

      customerType: '客户'
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.queryTable()
    },

    queryTable() {
      const params = Object.assign({}, this.queryForm)
      this.queryUserInfoList(params)
    },

    grade(row) {
      const json = row.userInfoProperty
      const params = Object.assign({}, row)
      params.userInfoProperty = json

      // 计算评分
      params.userScore = this.calculateScore(params)

      this.saveUserInfo(params)
    },

    // 提交用户信息
    submitUserInfo() {
      const json = JSON.stringify(this.dialog.add.form.userInfoProperty)
      const params = Object.assign({}, this.dialog.add.form)
      params.userInfoProperty = json

      // 计算评分
      params.userScore = this.calculateScore(params)

      this.saveUserInfo(params)
    },

    // 计算分数
    calculateScore(params) {
      let score = 0
      // 获取评级指标
      const obj = JSON.parse(params.userInfoProperty)
      // 判断客户类型
      // 个人 年龄，学历，住房，婚姻，财产，家庭年实际收入，诚信度
      if (params.userType === '个人') {
        const {
          age, education, HousingSituation
          , maritalStatus, bankDeposit, familyIncome
          , reputationEvaluation
        } = obj

        const sum = Number(age || 0) + Number(education || 0) + Number(HousingSituation || 0) +
          Number(maritalStatus || 0) + Number(bankDeposit || 0) + Number(familyIncome || 0) + Number(reputationEvaluation || 0)
        score = sum / 7
      } else {
        // 企业 产业稳定性，诚信度，企业不良记录，不动产，企业年收入，收益率，资本额度
        const {
          industrialStability, reputationEvaluation, badBankRecords
          , realEstateValuation, familyIncome, expectedYield
          , totalAssets
        } = obj

        const sum = Number(industrialStability || 0) + Number(reputationEvaluation || 0) + Number(badBankRecords || 0) +
          Number(realEstateValuation || 0) + Number(familyIncome || 0) + Number(expectedYield || 0) + Number(totalAssets || 0)
        score = sum / 7
      }

      return score.toFixed(2)
    },

    // 审核资质
    examine(row) {
      this.$confirm(`该客户资质评分为${row.userScore}分，是否通过审核？`, '审核资质', {
        confirmButtonText: '通过',
        cancelButtonText: '不通过',
        type: 'warning'
      }).then(() => {
        // 通过
        const params = { userInfoId: row.userId, userStatus: '1' }
        this.updateStatus(params)
      }).catch(() => {
        // 不通过
        const params = { userInfoId: row.userId, userStatus: '2' }
        this.updateStatus(params)
      })
    },

    // 查询客户信息
    async queryUserInfoList(params) {
      try {
        this.table.loading = true
        const res = await queryUserInfoList(params)
        this.table.data = res || {}
      } catch (e) {
        console.error(e)
      } finally {
        this.table.loading = false
      }
    },

    // 查询客户信息
    async queryUserInfo(params) {
      try {
        this.dialog.add.loading = true
        const res = await queryUserInfo(params)
        this.dialog.add.form = res
        const json = JSON.parse(res.userInfoProperty)
        this.$set(this.dialog.add.form, 'userInfoProperty', json)
      } catch (e) {
        console.error(e)
      } finally {
        this.dialog.add.loading = false
      }
    },

    // 修改审核状态
    async updateStatus(params) {
      try {
        this.table.loading = true
        await updateStatus(params)
        this.$message({ message: '操作成功！', type: 'success' })
      } catch (e) {
        console.error(e)
      } finally {
        this.table.loading = false
        this.queryTable()
      }
    },

    // 保存客户信息
    async saveUserInfo(params) {
      try {
        this.dialog.add.loading = true
        await saveUserInfo(params)
        this.$message({ message: '操作成功！', type: 'success' })
      } catch (e) {
        console.error(e)
      } finally {
        this.dialog.add.loading = false
        this.dialog.add.show = false
        this.queryTable()
      }
    },

    // 查询合同信息
    async selectContractInfo(params) {
      try {
        this.dialog.contract.loading = true
        const res = await selectContractInfo(params)
        this.dialog.contract.form = res || {}
      } catch (e) {
        console.error(e)
      } finally {
        this.dialog.contract.loading = false
      }
    },

    // 查询保单信息
    async userInsuranceInfo(params) {
      try {
        this.dialog.insurancePolicy.loading = true
        const res = await userInsuranceInfo(params)
        this.dialog.insurancePolicy.form = res
      } catch (e) {
        console.error(e)
      } finally {
        this.dialog.insurancePolicy.loading = false
      }
    },

    // 保存合同信息
    async saveContractInfo() {
      try {
        this.dialog.contract.loading = true
        const params = Object.assign({}, this.dialog.contract.form)

        params.usersInfoId = this.dialog.contract.row.userId

        await saveContractInfo(params)
        this.$message({ message: '操作成功！', type: 'success' })
      } catch (e) {
        console.error(e)
      } finally {
        this.dialog.contract.loading = false
        this.dialog.contract.show = false
        this.queryTable()
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.width-90 {
  width: 90%;
}

::v-deep .el-scrollbar__wrap {
  overflow: scroll;
  overflow-x: hidden !important;
  height: 100%;
}
</style>
